package com.shj.controller;

import com.shj.entity.SysArea;
import com.shj.entity.SysFamily;
import com.shj.entity.SysPerson;
import com.shj.service.SysAreaService;
import com.shj.service.SysFamilyService;
import com.shj.service.SysPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/biz/family")
public class SysFamilyController {
    @Autowired
    private SysFamilyService sysFamilyService;
    @Autowired
    private SysAreaService sysAreaService;
    @Autowired
    private SysPersonService sysPersonService;

    /**
     * 查询家庭档案信息
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String findAll(Model model) {
        List<SysFamily> list = sysFamilyService.findAll();
        model.addAttribute("list", list);
        return "family/family_list";
    }

    /**
     * 打开添加家庭档案信息页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/openAdd")
    public String openAdd(Model model, HttpServletRequest request) {
        String agenCode = (String) request.getSession().getAttribute("agenCode");
        List<SysArea> list = sysAreaService.findChildArea(agenCode, 3);
        model.addAttribute("list", list);
        return "family/family_add";
    }

    /**
     * 添加家庭信息
     * @param model
     * @param sysFamily
     * @param sysPerson
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model, SysFamily sysFamily, SysPerson sysPerson, HttpServletRequest request) {
        // 经办人
        String creator = (String) request.getSession().getAttribute("userId");
        sysFamily.setCreator(creator);
        sysFamilyService.add(sysFamily, sysPerson);
        return findAll(model);
    }

    /**
     * 打开添加成员信息页面
     * @param model
     * @param code
     * @return
     */
    @RequestMapping("/openAddMember")
    public String openAddMember(Model model, String code) {
        model.addAttribute("code", code);
        return "family/family_addMember";
    }

    /**
     * 添加家庭成员
     * @param model
     * @param sysPerson
     * @return
     */
    @RequestMapping("/addMember")
    public String addMember(Model model, SysPerson sysPerson) {
        sysPersonService.addMember(sysPerson);
        return findAll(model);
    }

    /**
     * 打开修改家庭信息页面
     * @param model
     * @param code
     * @return
     */
    @RequestMapping("/openEdit")
    public String openEdit(Model model, String code, HttpServletRequest request){
        String agenCode = (String) request.getSession().getAttribute("agenCode");
        List<SysArea> list = sysAreaService.findChildArea(agenCode, 3);
        model.addAttribute("list", list);
        SysFamily sysFamily = sysFamilyService.findByFamilyCode(code);
        model.addAttribute("family", sysFamily);
        return "family/family_edit";
    }


    @RequestMapping("/edit")
    public String edit(Model model, SysFamily sysFamily) {
        sysFamilyService.edit(sysFamily);
        return findAll(model);
    }

    /**
     * 删除
     * @param model
     * @param code
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Model model, String code) {
        sysFamilyService.delete(code);
        return findAll(model);
    }
}
